<template>
	<w-dailog
		v-model="dialogVisible"
		width="1000px"
		:title="title"
		heightFullscreen
		:showMoreBtn="false"
		:beforeClose="onBeforeClose"
		@close="onRefresh"
	>
		<div class="create-organ" slot-scope="{ fullScreen }">
			<w-scroll-bar :flex="fullScreen">
				<basicInfo
					ref="basicInfo"
					:visibleFlag="dialogVisible"
					:routeDetails="routeDetails"
					:disabled="canEdit"
					:materialNameList="materialNameList"
					:productionLineList="productionLineList"
					@productionLineChange="productionLineChange"
				></basicInfo>
				<detailsInfo
					ref="detailsInfo"
					:visibleFlag="dialogVisible"
					:routesTable="routesTable"
					:routeDetails="routeDetails"
					:otherChanged='otherChanged'
					:disabled="canEdit"
					@addRoute="addRoute"
					@reduceProcess="reduceProcess"
					@clickProcess="clickProcess"
				></detailsInfo>
				<procedureInfo
					ref="procedureInfo"
					:visibleFlag="dialogVisible"
					:procedureDetails="procedureDetails"
					:procedureNameList="procedureNameList"
					:itemList="itemList"
					:routeDetails="routeDetails"
					:proItemList="proItemList"
					:disabled="canEdit"
					:fSaveProcedureUse="saveProcedure"
					:fSaveProcedurePro="saveProcedure"
					@procedureChange="procedureChange"
					@saveProcedure="saveProcedure"
				></procedureInfo>
				<fxCopy :hasTree="false" :copyTableData="copyTableData"
					ref="copyDialog"
					:columns="copyColumns"
					:canAdd="false"
					title="复制工艺路线"
					@on-copysave="copySave">
				</fxCopy>
			</w-scroll-bar>
		</div>
		<template slot="footer">
			<el-button type="primary" @click="onCopyClick" v-if='isEdit'>复制</el-button>
			<el-button @click="onCancelClick">取消</el-button>
			<el-button type="primary" @click="onSaveClick">保存</el-button>
		</template>
	</w-dailog>
</template>
<script>
import basicInfo from './BasicInfo.vue'
import detailsInfo from './DetailsInfo.vue'
import procedureInfo from './ProcedureInfo.vue'
import fxCopy from '@/components/common/Fx-copy.vue'
export default {
	name: 'createContainer',
	components: {
		basicInfo,
		detailsInfo,
		procedureInfo,
		fxCopy
	},
	data () {
		return {
			title: '',
			dialogVisible: false,
			routeDetails: {},
			routesTable: [],
			materialNameList: [],
			procedureDetails: {
				connectionType: 1,
				expendableUrls: [],
				fileUrls: [],
				id: 'temp',
				lineId: '',
				name: '',
				parentId: '',
				procedureId: '',
				productionUrls: [],
				routingSopDTO: {
					id: '',
					packingRequirement: '',
					performanceStandard: '',
					processingRequirement: '',
					qualityRequirements: '',
					remark: ''
				},
				seq: '1',
				stationEntitys: [],
				subId: '',
				timeUnit: 1,
				workTimeCount: ''
			},
			procedureNameList: [],
			itemList: [], // 消耗品
			proItemList: [],
			fxDataVerification: this.$fxUtils.fxDataVerification(),
			nextCode: '',
			route: {},
			otherChanged: [],
			copyTableData: [],
			isEdit: false,
			copyColumns: [{
				label: '产成品',
				prop: 'materialName',
				width: '150px',
				required: true
			}, {
				label: '工艺路线编号',
				prop: 'code',
				width: '150px',
				required: true
			}, {
				label: '工艺路线版本编号',
				prop: 'version',
				width: '150px',
				required: true
			}],
			productionLineList: []
		}
	},
	computed: {
		isAdd () {
			return !this.routeDetails.id
		},
		allData () {
			return {
				routeDetails: this.routeDetails,
				procedureDetails: this.procedureDetails
			}
		},
		canEdit () {
			return !this.routeDetails.updateFlag
		}
	},
	methods: {
		async add (treeNode) {
			this.isEdit = false
			this.otherChanged = []
			const productionLine = await this.getListData()
			this.routeDetails = {
				id: '',
				code: this.nextCode,
				materialName: '',
				version: this.$fxUtils.setDate(new Date(), 'noSymbol'),
				effectDate: this.$fxUtils.setDateTime(new Date()),
				enable: 1,
				productionLineId: (productionLine || [])[0].id,
				remark: '',
				updateFlag: true
			}
			this.tableDetails = []
			this.routesTable = [{
				connectionType: 1,
				expendableUrls: [],
				fileUrls: [],
				id: 'temp',
				lineId: '',
				name: '',
				parentId: '',
				procedureId: '',
				productionUrls: [],
				routingSopDTO: {
					id: '',
					packingRequirement: '',
					performanceStandard: '',
					processingRequirement: '',
					qualityRequirements: '',
					remark: ''
				},
				seq: '1',
				stationEntitys: [],
				subId: '',
				timeUnit: 1,
				workTimeCount: ''
			}]
			this.procedureDetails = {
				connectionType: 1,
				expendableUrls: [],
				fileUrls: [],
				id: 'temp',
				lineId: '',
				name: '',
				parentId: '',
				procedureId: '',
				productionUrls: [],
				routingSopDTO: {
					id: '',
					packingRequirement: '',
					performanceStandard: '',
					processingRequirement: '',
					qualityRequirements: '',
					remark: ''
				},
				seq: '1',
				stationEntitys: [],
				subId: '',
				timeUnit: 1,
				workTimeCount: ''
			}
			this.fxDataVerification.resetData(this.allData)
			// this.fxDataVerification.resetData(this.procedureDetails)
			this.title = '新增工艺路线'
			this.dialogVisible = true
			if (this.routesTable.length >= 1) {
				this.$nextTick(() => {
					const noSave = true
					this.$refs.detailsInfo.clickProcess(this.routesTable[0], noSave)
					this.$refs.detailsInfo.drawBoxPath()
				})
			}
		},
		// 工序改变 工艺路由名称赋值
		procedureChange (val, procedureId) {
			const filterName = this.procedureNameList.filter(n => {
				if (n.id === val) {
					return n
				}
			})
			this.routesTable.map(p => {
				if (p.id === procedureId) {
					p.name = filterName[0].name
				}
			})
		},
		// 新建路由
		addRoute (currentRoute) {
			this.onSaveAndAddClickHandler(currentRoute).then(() => {
				// this.$refs.detailsInfo.addOneProcess(currentRoute)
			})
		},
		onSaveAndAddClickHandler (currentRoute) {
			return this.onValidateFormData().then(() => {
				return this.$refs.procedureInfo.checkProcedureDetail().then(() => {
					return this.onSaveOrganHandler().then(() => {
						this.onSaveProcedureHandler().then((res) => {
							this.routesTable.map(v => {
								if (res.id === v.id) {
									v.seq = res.seq
									v.procedureId = res.procedureId
									v.connectionType = res.connectionType
									v.workTimeCount = res.workTimeCount
									v.timeUnit = res.timeUnit
									v.stationEntitys = res.stationEntitys
								}
							})
							this.updateOther(currentRoute.id)
							this.backWriteTempOne(this.otherChanged, res.id)
							this.clickProcess(res, true).then(() => {
								this.$refs.detailsInfo.addOneProcess(currentRoute)
							})
						})
					})
				})
			})
		},
		// 删除路由
		reduceProcess (currentRoute) {
			this.onDeleteClickHandler(currentRoute)
		},
		// 点击路由
		clickProcess (currentRoute, flag) {
			if (flag) {
				return this.renderDetails(currentRoute)
			} else {
				return this.saveProcedure().then(() => {
					this.checkData().then(() => {
						this.renderDetails(currentRoute)
					})
				})
			}
		},
		renderDetails (currentRoute) {
			return new Promise(resolve => {
				if (currentRoute) {
					this.selectProcess(currentRoute.id)
					const routingSopDTO = {
						productionUrls: [],
						expendableUrls: [],
						fileUrls: [],
						id: '',
						packingRequirement: '',
						performanceStandard: '',
						processingRequirement: '',
						qualityRequirements: '',
						remark: '',
						routingId: 0
					}
					this.$set(this.procedureDetails, 'routingSopDTO', routingSopDTO)
					Object.assign(this.procedureDetails, currentRoute)
					const id = currentRoute.id === 'temp' ? '' : currentRoute.id
					if (id) {
						return Promise.all([this.$refs.procedureInfo.refreshPro(), this.$refs.procedureInfo.refreshProUse(), this.getProcedureSOPInfo(id), this.$refs.procedureInfo.getProcedureSOPFile(id), this.$refs.procedureInfo.getProcedureSOPImg(id)]).then(() => {
							this.fxDataVerification.resetData(this.allData)
							resolve()
						})
					} else {
						return Promise.all([this.$refs.procedureInfo.refreshPro(), this.$refs.procedureInfo.refreshProUse()]).then(() => {
							this.fxDataVerification.resetData(this.allData)
							resolve()
						})
					}
				}
			})
		},
		selectProcess (id) {
			this.$refs.detailsInfo.selectProcess(id)
		},
		// 获取路由工序
		getRoute (id) {
			this.findStationAllByRoutingId(id).then(this.renderRoute)
		},
		// 通过参数查询路由列表接口
		findUnitListByParam (id) {
			return new Promise(resolve => {
				this.$fxApi('routing.findUnitListByParam')({
					query: {
						id: id
					}
				}).then(res => {
					resolve(res)
				})
			})
		},
		renderRoute (res) {
			this.route = res
		},
		// 保存当前工序按钮
		saveProcedure () {
			// return this.checkSameProduct().then(() => {
			return this.onValidateFormData().then(() => {
				return this.$refs.procedureInfo.checkProcedureDetail().then(() => {
					// return this.checkSameProduct().then(() => {
					return this.onSaveOrganHandler().then(() => {
						return this.onSaveProcedureHandler().then((res) => {
							this.routesTable.map(v => {
								if (res.id === v.id) {
									// v.seq = res.seq
									v.procedureId = res.procedureId
									v.connectionType = res.connectionType
									v.workTimeCount = res.workTimeCount
									v.timeUnit = res.timeUnit
									v.stationEntitys = res.stationEntitys
								}
							})
							// this.updateOther(res.id, true)
							this.$refs.detailsInfo.selectProcess(res.id)
						})
					})
				})
			})
		},
		async edit (routeLineDetail, routedetails) {
			this.isEdit = true
			this.otherChanged = []
			await this.renderRouteLineAndDetail(routeLineDetail, routedetails)
			const { id, materialId, code, version, effectDate, enable, productionLineId, remark, updateFlag } = routeLineDetail
			this.routeDetails = {
				id,
				materialId,
				code,
				version,
				effectDate,
				enable,
				productionLineId,
				remark,
				updateFlag
			}
			if (routedetails.length >= 1) {
				this.routesTable = routedetails
				this.procedureDetails = routedetails[0]
				this.procedureChange(routedetails[0].procedureId, routedetails[0].id)
				this.routesTable.map(s => {
					for (let i = 0, l = this.procedureNameList.length; i < l; i++) {
						if (this.procedureNameList[i].id === s.procedureId) {
							s.name = this.procedureNameList[i].name
						}
					}
				})
			} else {
				this.routesTable = [{
					id: 'temp',
					name: '',
					level: 1,
					seq: 1,
					parentId: '',
					subId: '',
					active: '1'
				}]
				this.procedureDetails = {
					connectionType: 1,
					expendableUrls: [],
					fileUrls: [],
					id: 'temp',
					lineId: this.routeDetails.id,
					name: '',
					parentId: '',
					procedureId: '',
					productionUrls: [],
					routingSopDTO: {
						id: '',
						packingRequirement: '',
						performanceStandard: '',
						processingRequirement: '',
						qualityRequirements: '',
						remark: ''
					},
					seq: 1,
					stationEntitys: [],
					subId: '',
					timeUnit: 1,
					workTimeCount: ''
				}
			}
			this.fxDataVerification.resetData(this.allData)
			this.title = '编辑工艺路线'
			this.dialogVisible = true
			if ((this.routesTable || []).length >= 1) {
				this.$nextTick(() => {
					const noSave = true
					this.$refs.detailsInfo.clickProcess(this.routesTable[0], noSave)
					this.$refs.detailsInfo.drawBoxPath()
				})
			}
		},
		async getListData () {
			return Promise.all([this.getNextCode(), this.getMaterialNameList(), this.getProcedureSel(), this.getProductionLineList()]).then(([nextCode, materialName, allprocedure, productionLine]) => {
				this.nextCode = nextCode
				this.renderList(materialName, allprocedure, productionLine)
				return productionLine
			})
		},
		async renderRouteLineAndDetail (routeLineDetail, routedetails) {
			return Promise.all([this.getMaterialNameList(), this.getProcedureSel(), this.getProductionLineList()]).then(([materialName, allprocedure, productionLine]) => {
				this.renderList(materialName, allprocedure, productionLine)
			})
		},
		getNextCode () {
			return this.$fxApi('routing.getNextCode')
		},
		getMaterialNameList () {
			return this.$fxApi('material.findMeterialByType')({
				query: {
					processingFlag: 0
				}
			})
		},
		getProductionLineList () {
			return this.$fxApi('routing.findProductionLineListAll').then(res => {
				res.map(v => {
					v.name = v.productionGroupName
				})
				return res
			})
		},
		getProItemList () {
			return this.$fxApi('material.findMeterialByType')({
				query: {
					processingFlag: 2
				}
			})
		},
		// 获取工序下拉
		getProcedureSel () {
			return this.$fxApi('procedure.findListAll')
		},
		// 获取工序 消耗品 产出品 列表
		getProcedureUseAndPro (routingId, dataType) {
			return this.$fxApi('procedure.findListByParam')({
				query: {
					dataType: dataType,
					routingId: routingId
				}
			})
		},
		onRefresh () {
			this.$emit('on-refresh')
		},
		verificationDetails () {
			return this.fxDataVerification.checkComfirm(this.allData)
		},
		checkData () {
			return this.$refs.procedureInfo.pCheckDetailsUnSaving()
		},
		onSaveClick () {
			this.saveProcedure().then(() => {
				if (this.fxDataVerification.checkData(this.allData)) {
					this.checkSameProduct().then(() => {
						this.$refs.procedureInfo.pCheckDetailsUnSaving().then(() => {
							this.$fxMessage.success('保存成功')
						})
					})
				} else {
					this.checkSameProduct().then(() => {
						this.$refs.procedureInfo.pCheckDetailsUnSaving().then(() => {
							this.onSaveClickHandler()
						})
					})
				}
			})
		},
		onSaveClickHandler (currentRoute) {
			return this.onValidateFormData().then(() => {
				return this.$refs.procedureInfo.checkProcedureDetail().then(() => {
					return this.onSaveOrganHandler().then(() => {
						return this.onSaveProcedureHandler()
						// return this.checkProcedureHasProduct()
					})
				})
			})
		},
		onDeleteClickHandler (currentRoute) {
			var one = (currentRoute.subId || '') === ''	? ((currentRoute.parentId || '') === ''	? false	: this.$refs.detailsInfo.getOneNode(currentRoute.parentId)) : this.$refs.detailsInfo.getOneNode(currentRoute.subId)
			// 删除是如果是没有id,也就是说没有保存过的工序可以直接删除不用调接口
			if (currentRoute.id !== 'temp') {
				this.checkDelRoute(currentRoute).then(res => {
					this.deleteRoute(currentRoute).then(res => {
						// this.reduceProcess(currentRoute)
						this.$refs.detailsInfo.reduceOneProcess(currentRoute)
						this.updateOther(currentRoute.id)
						if (one[0]) {
							this.$refs.detailsInfo.clickProcess(one[0], true)
						}
					})
				})
			} else {
				// this.reduceProcess(currentRoute)
				this.otherChanged = []
				this.$refs.detailsInfo.reduceOneProcess(currentRoute)
				if (one[0]) {
					this.$refs.detailsInfo.clickProcess(one[0], true)
				}
			}
		},
		checkDelRoute (currentRoute) {
			const id = currentRoute.id
			return new Promise(resolve => {
				this.$fxApi('routing.checkDelete', id).then(res => {
					resolve(res)
				})
			})
		},
		deleteRoute (currentRoute) {
			const id = currentRoute.id
			return new Promise(resolve => {
				this.$fxApi('routing.delete', id).then(res => {
					resolve(res)
				})
			})
		},
		onValidateFormData () {
			return new Promise(resolve => {
				this.$refs.basicInfo.validate().then(() => {
					resolve()
				}).catch(() => {
					this.$fxMessage.warning('请填写必填信息')
				})
			})
		},
		// 路由最后一个成品中是否包含和主表选择的成品相同的成品，如果没有的话，给出提示“未添加成品【xxxx】的工序路由，是否确定保存
		checkSameProduct () {
			const materialName = this.materialNameList.filter(p => {
				if (p.id === this.routeDetails.materialId) {
					return p
				}
			})
			return new Promise(resolve => {
				this.$refs.procedureInfo.checkSameProduct(this.procedureDetails, this.routeDetails.materialId).then(() => {
					resolve()
				}).catch(() => {
					if (materialName) {
						if (materialName.length > 0) {
							this.$fxConfirm({ message: `未添加成品【${materialName[0].name}】的工序路由，是否确定保存？` }).then(() => {
								resolve()
							}).catch(() => {
								// this.dialogVisible = false
							})
						}
					}
				})
			})
		},
		checkProcedureHasProduct (currentRoute) {
			return new Promise(resolve => {
				this.$refs.procedureInfo.checkProcedureHasProduct(currentRoute, this.routeDetails.materialId).then(() => {
					resolve()
				}).catch(() => {
					this.$fxMessage.warning('路由中已经存在产出品，不能再新建路由')
				})
			})
		},
		onSaveOrganHandler () {
			const postData = {
				...this.routeDetails
			}
			if (this.isAdd) {
				return this.$fxApi('routing.lineSaveOrUpdate')({ data: postData }).then(res => {
					this.$set(this.routeDetails, 'id', res.data.id)
					this.$set(this.procedureDetails, 'lineId', res.data.id)
					this.fxDataVerification.resetDataObject(this.allData, 'routeDetails')
					return Promise.resolve(res)
				})
			} else {
				if (this.fxDataVerification.checkDataObject('routeDetails', this.routeDetails)) {
					return Promise.resolve(this.routeDetails)
				} else {
					return this.$fxApi('routing.lineSaveOrUpdate')({ data: postData }).then(res => {
						this.fxDataVerification.resetDataObject(this.allData, 'routeDetails')
						return Promise.resolve(res)
					})
				}
			}
		},
		// 保存工序信息 包括基本信息 图片信息等
		onSaveProcedureHandler () {
			const { connectionType, expendableUrls, fileUrls, id, lineId, name, parentId, procedureId, productionUrls, seq, stationIds, subId, timeUnit, workTimeCount, routingSopDTO } = this.procedureDetails
			const postData = {
				connectionType,
				expendableUrls,
				fileUrls,
				id,
				lineId,
				name,
				parentId: parentId === 'temp' ? '' : parentId,
				procedureId,
				productionUrls,
				seq,
				stationIds,
				subId: subId === 'temp' ? '' : subId,
				timeUnit,
				workTimeCount,
				routingSopDTO
			}
			postData.expendableUrls = postData.routingSopDTO.expendableUrls || []
			postData.productionUrls = postData.routingSopDTO.productionUrls || []
			postData.fileUrls = postData.routingSopDTO.fileUrls || []
			// 关联工位
			if (this.procedureDetails.stationEntitys) {
				postData.stationIds = this.procedureDetails.stationEntitys.map(s => {
					return s.id
				})
			}
			if (this.procedureDetails.id === 'temp') {
				postData.id = ''
				return this.$fxApi('routing.saveOrUpdate')({ data: postData }).then(res => {
					this.$set(this.procedureDetails, 'id', res.id)
					this.fxDataVerification.resetDataObject(this.allData, 'procedureDetails')
					this.backWriteTempOne(this.otherChanged, res.id)
					this.updateOther(res.id)
					this.backWriteTempOne(this.routesTable, res.id)
					this.getProcedureSOPInfo(res.id)
					return Promise.resolve(res)
				})
			} else {
				if (this.fxDataVerification.checkDataObject('procedureDetails', this.procedureDetails)) {
					return Promise.resolve(this.procedureDetails)
				} else {
					return this.$fxApi('routing.saveOrUpdate')({ data: postData }).then(res => {
						this.fxDataVerification.resetDataObject(this.allData, 'procedureDetails')
						this.backWriteTempOne(this.otherChanged, res.id)
						this.updateOther(res.id)
						this.backWriteTempOne(this.routesTable, res.id)
						this.getProcedureSOPInfo(res.id)
						return Promise.resolve(res)
					})
				}
			}
		},
		// 获取路由工序 SOP信息
		getProcedureSOPInfo (routingId) {
			return this.$fxApi('routing.sopFindListByParam')({
				query: {
					routingId: routingId
				}
			}).then(res => {
				// this.procedureDetails.id = res.id
				// this.procedureDetails.packingRequirement = ''
				this.procedureDetails = {
					...this.procedureDetails,
					routingSopDTO: {
						productionUrls: this.procedureDetails.routingSopDTO.productionUrls,
						expendableUrls: this.procedureDetails.routingSopDTO.expendableUrls,
						fileUrls: this.procedureDetails.routingSopDTO.fileUrls,
						packingRequirement: res.packingRequirement,
						performanceStandard: res.performanceStandard,
						processingRequirement: res.processingRequirement,
						qualityRequirements: res.qualityRequirements,
						remark: res.remark,
						id: res.id
					}
				}
				return Promise.resolve(res)
			})
		},
		backWriteTempOne (data, backId) {
			// 回写数据
			data.map(p => {
				if (p.id === 'temp') {
					p.id = backId
				} else {
					if (p.parentId === 'temp') {
						p.parentId = backId
					}
					if (p.subId === 'temp') {
						p.subId = backId
					}
				}
			})
		},
		updateOther (rid, flag) {
			if (this.otherChanged.length === 0) {
				return
			}
			// const postDataArr = JSON.parse(JSON.stringify(this.otherChanged).replace('temp', rid))
			var postData = []
			this.otherChanged.map(p => {
				if (!flag) {
					if (p.subId !== 'temp' && p.parentId !== 'temp' && p.id !== 'temp') {
						postData.push({
							id: p.id,
							parentId: p.parentId || '',
							seq: p.seq,
							subId: p.subId || ''
						})
					}
				} else {
					if (p.subId === 'temp') {
						p.subId = rid
					}
					if (p.parentId === 'temp') {
						p.parentId = rid
					}
					if (p.id === 'temp') {
						p.id = rid
					}
					postData.push({
						id: p.id,
						parentId: p.parentId || '',
						seq: p.seq,
						subId: p.subId || ''
					})
				}
				// }
			})
			if (postData.length === 0) {
				return
			}
			return this.$fxApi('routing.updateOther')({ data: postData }).then(() => {
				this.otherChanged = []
			})
		},
		onCancelClick () {
			this.verificationDetails().then(() => {
				this.onSaveClickHandler().then(() => {
					this.dialogVisible = false
				})
			}).catch(() => {
				this.dialogVisible = false
			})
		},
		onBeforeClose (close) {
			this.verificationDetails().then(() => {
				this.onSaveClickHandler().then(close)
			}).catch(() => {
				this.dialogVisible = false
			})
		},
		renderList (materialName, allprocedure, productionLine) {
			this.materialNameList = materialName.filter(p => {
				if (p.enable === 1 && p.processingFlag === 1) {
					return p
				}
			})
			this.itemList = materialName.filter(p => {
				if (p.enable === 1 && (p.processingFlag === 2 || p.processingFlag === 0)) {
					return p
				}
			})
			this.proItemList = materialName.filter(p => {
				if (p.enable === 1 && (p.processingFlag === 1 || p.processingFlag === 0)) {
					return p
				}
			})
			this.procedureNameList = allprocedure.filter(p => {
				if (p.enable === 1) {
					return p
				}
			})
			this.productionLineList = productionLine
		},
		changeList () {
			this.$refs.detailsInfo.drawOneLine()
		},
		// 复制
		onCopyClick () {
			this.copyTableData = []
			this.copyLineList(this.procedureDetails.lineId).then(res => {
				this.copyTableData = res
				this.$refs.copyDialog.open()
			})
		},
		// 复制列表
		copyLineList (id) {
			return new Promise(resolve => {
				this.$fxApi('routing.copyList')({
					query: {
						id: id
					}
				}).then(res => {
					resolve(res)
				})
			})
		},
		copySave (selData) {
			return this.$fxCommon.orderListFrontFunc('copyMulity', selData)
				.then(this.doCopySave)
				.then(this.closeDialog)
		},
		closeDialog () {
			this.$refs.copyDialog.onCancelClick()
		},
		doCopySave (data) {
			const copyIds = data.map(d => {
				return d.id
			})
			return new Promise(resolve => {
				this.$fxApi('routing.copyLine')({
					data: {
						copyId: copyIds,
						currentId: this.procedureDetails.lineId
					}
				}).then(res => {
					resolve(res)
				})
			})
		},
		productionLineChange (value) {
			const oldValue = (this.routeDetails || {}).productionLineId
			// const newValue = value
			if (!this.newOneRoute()) {
				this.clearAndAdd()
				this.$refs.detailsInfo.clearoneD()
			} else {
				this.$fxConfirm('更改产线将会清除路由信息,是否要继续').then(() => {
					this.clearAndAdd()
					this.$refs.detailsInfo.clearoneD()
				}).catch(() => {
					if (this.routeDetails) {
						(this.routeDetails || {}).productionLineId = oldValue
					}
				})
			}
		},
		newOneRoute () {
			if (this.routesTable.length === 1) {
				if (this.routesTable[0].id === 'temp') {
					if (this.procedureDetails.connectionType !== 1) return true
					if (this.procedureDetails.expendableUrls.length !== 0) return true
					if (this.procedureDetails.fileUrls.length !== 0) return true
					if (this.procedureDetails.id !== 'temp') return true
					if (this.procedureDetails.lineId !== '') return true
					if (this.procedureDetails.name !== '') return true
					if (this.procedureDetails.parentId !== '') return true
					if (this.procedureDetails.procedureId !== '') return true
					if (this.procedureDetails.productionUrls.length !== 0) return true
					if (this.procedureDetails.routingSopDTO.id) return true
					if (this.procedureDetails.routingSopDTO.packingRequirement) return true
					if (this.procedureDetails.routingSopDTO.performanceStandard) return true
					if (this.procedureDetails.routingSopDTO.processingRequirement) return true
					if (this.procedureDetails.routingSopDTO.qualityRequirements) return true
					if (this.procedureDetails.routingSopDTO.remark) return true
					if (this.procedureDetails.stationEntitys.length !== 0) return true
					if (this.procedureDetails.timeUnit !== 1) return true
					if (this.procedureDetails.workTimeCount) return true
				} else {
					return true
				}
			} else {
				return true
			}
			return false
		},
		clearAndAdd () {
			this.routesTable = [{
				id: 'temp',
				name: '',
				level: 1,
				seq: 1,
				parentId: '',
				subId: '',
				active: '1'
			}]
			this.procedureDetails = {
				connectionType: 1,
				expendableUrls: [],
				fileUrls: [],
				id: 'temp',
				lineId: this.routeDetails.id,
				name: '',
				parentId: '',
				procedureId: '',
				productionUrls: [],
				routingSopDTO: {
					id: '',
					packingRequirement: '',
					performanceStandard: '',
					processingRequirement: '',
					qualityRequirements: '',
					remark: ''
				},
				seq: 1,
				stationEntitys: [],
				subId: '',
				timeUnit: 1,
				workTimeCount: ''
			}
			this.otherChanged = []
		}
	},
	watch: {
		procedureDetails: {
			immediate: true,
			handler (data) {
				if (data) {
					this.routesTable.map(v => {
						if (data.id === v.id) {
							v.seq = data.seq
							v.procedureId = data.procedureId
							v.connectionType = data.connectionType
							v.workTimeCount = data.workTimeCount
							v.timeUnit = data.timeUnit
							v.stationEntitys = data.stationEntitys
							this.otherChanged.map(p => {
								if (data.id === p.id) {
									p.parentId = data.parentId
									p.subId = data.subId
								}
							})
						}
					})
				}
			},
			deep: true
		}
	}
}
</script>
<style lang="stylus" scoped>
@import '~$assets/stylus/varsty.styl'
.create-organ {
	flex: 1
	display: flex;
	flex-direction: column;
	overflow: auto;
}
</style>
